Skip to content

fix(risk): harden DCA comparator comparability and SMA regime fail-closed guards - #823

Merged
Pigbibi merged 2 commits into
mainfrom
fix/benchmark-dca-regime-audit
Nov 28, 2026
Merged

Pigbibi merged 2 commits into
mainfrom
fix/benchmark-dca-regime-audit

Conversation

@Pigbibi

@Pigbibi Pigbibi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

AI assistance

This PR was drafted with AI coding assistance and reviewed locally via pytest, ruff, and Codex adversarial audit findings before pushing.

What changed

  • Tighten SmaHysteresisRegimeConfig.sma_window validation in src/quant_platform_kit/risk/benchmark_dca_and_regime.py to require isinstance(sma_window, int) (rejecting booleans and floats like 200.0).
  • Add is_valid: bool = True to SmaHysteresisRegimeDecision and force exposure_multiplier = 0.0 (with is_valid=False) on INSUFFICIENT_HISTORY_FAIL_CLOSED and INVALID_PRICE_FAIL_CLOSED, keeping invalid-data fail-closed strictly separate from valid BELOW_LOWER_SMA_BAND (cfg.risk_off_multiplier).
  • Add explicit valuation_session_date validation on evaluate_matched_cashflow_dca_benchmark / MatchedCashflowDcaReport, separate total_deposits_usd and total_withdrawals_usd, return None for undefined multiples/percentages when net invested or benchmark ending value <= 0, and require comparable (total_deposits_usd > 0 and ending_value_usd > 0 across all benchmarks) before beats_all_benchmarks can be True.
  • Expand tests/test_benchmark_dca_and_regime.py to cover float sma_window rejection, non-zero risk_off_multiplier vs invalid-data fail-closed isolation, valuation-date mismatch rejection, and zero-cashflow comparability.

How verified

  • uv run pytest tests/test_benchmark_dca_and_regime.py tests/test_twr_capital_ladder.py -q -> 9 passed in 3.07s
  • uv run ruff check src/quant_platform_kit/risk/benchmark_dca_and_regime.py tests/test_benchmark_dca_and_regime.py -> All checks passed!

Why

Address all P1/P2 findings from the post-merge audit on benchmark_dca_and_regime.py so zero-cashflow benchmarks cannot produce false wins, valuation dates stay synchronized fail-closed, and invalid price/history inputs always drop exposure to 0.0 regardless of risk_off_multiplier.

Risk

  • Risk level: R2 (adds library risk-reporting module guards and unit tests; zero runtime/order side effects, live=OFF, paper=OFF).
  • Breaking impact: Additive/hardening fields (is_valid, valuation_session_date, total_deposits_usd, total_withdrawals_usd, comparable, nullable ending_multiple / alpha_vs_benchmark_pct) only affect new callers of the module merged in #822.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T21:15:14.559340Z a248875 Manual request
🔒 Security Review ✅ Completed 2026-11-28T21:16:34.864272Z a248875 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Pigbibi
Pigbibi force-pushed the fix/benchmark-dca-regime-audit branch from c5adcb2 to 1b497d2 Compare November 28, 2026 21:03
@Pigbibi

Pigbibi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@qsl-gemini-reviewer

Copy link
Copy Markdown

AI Code Review (gemini / gemini-4-neon)

The PR cleanly hardens DCA comparability, valuation-date synchronization, SMA window type validation, and fail-closed exposure handling on invalid/insufficient regime inputs. No high or medium severity issues were found.

Reviewed commit: 1b497d22d4 · Coverage: complete · Unresolved findings: false

Findings

  • [LOW] src/quant_platform_kit/risk/benchmark_dca_and_regime.py: Changing BenchmarkDcaResult.ending_multiple and alpha_vs_benchmark_pct to float | None and adding/reordering required fields on BenchmarkDcaResult and MatchedCashflowDcaReport is a breaking contract change for any downstream consumers formatting those fields directly or constructing the dataclasses positionally; ensure a coordinated rollout note accompanies QPK_PIN bumps.
  • [LOW] tests/test_benchmark_dca_and_regime.py: Consider adding unit tests for an invalid valuation_session_date format (non-ISO string or non-string) and for the profitable net-withdrawal case (total_deposits_usd > 0, total_invested_usd <= 0, ending_value_usd > 0) to verify ending_multiple is None while comparable remains True.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 1b497d22d4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 1b497d22d4

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@Pigbibi

Pigbibi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: a2488755d0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Pigbibi Pigbibi added the agent-approved An agent cleared this high-risk change on the current head; not an owner confirmation label Nov 28, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: a2488755d0

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@qsl-gemini-reviewer

Copy link
Copy Markdown

AI Code Review (gemini / gemini-4-neon)

The PR cleanly hardens DCA benchmark comparability, valuation-date synchronization, floating-point dust handling, and SMA hysteresis fail-closed guards. No high- or medium-severity correctness, security, or boundary defects were found; only minor low-severity notes regarding downstream compatibility for the newly nullable result fields and one untested error branch.

Reviewed commit: a2488755d0 · Coverage: complete · Unresolved findings: false

Findings

  • [LOW] src/quant_platform_kit/risk/benchmark_dca_and_regime.py: BenchmarkDcaResult.ending_multiple and alpha_vs_benchmark_pct now return float | None, and new required fields were added to BenchmarkDcaResult and MatchedCashflowDcaReport. While appropriate for fixing the zero-cashflow/zero-ending-value bugs, ensure any downstream consumers updating QPK_PIN handle None values and do not instantiate these dataclasses positionally.
  • [LOW] tests/test_benchmark_dca_and_regime.py: Consider adding a unit test case for a malformed valuation_session_date (e.g. non-ISO string) in test_matched_cashflow_dca_fails_closed_on_bad_inputs to explicitly cover the invalid ISO date ValueError branch.

@Pigbibi
Pigbibi merged commit 65aad08 into main Nov 28, 2026
14 of 16 checks passed
@Pigbibi
Pigbibi deleted the fix/benchmark-dca-regime-audit branch November 28, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-approved An agent cleared this high-risk change on the current head; not an owner confirmation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant